package com.itheima.service.Impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.exception.CustomerExceptionHandler;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.PageBean;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;

    /**
     * 查询所有班级
     * @return 返回查询到所有班级的集合
     */
    @Override
    public PageBean selectAllClazz(ClazzQueryParam clazzQueryParam) {

        //设置分页条件
        PageHelper.startPage(clazzQueryParam.getPage(), clazzQueryParam.getPageSize());
        Page<Clazz> clazzes = (Page<Clazz>) clazzMapper.selectAllClazz(clazzQueryParam);

        return new PageBean(clazzes.getTotal(), clazzes.getResult());

    }

    /**
     * 添加班级
     * @param clazz
     */
    @Override
    public void addClazz(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.addClazz(clazz);
    }

    /**
     * 根据id查询班级
     *
     * @param id 前端传回的id
     * @return 返回查询到的班级
     */
    @Override
    public Clazz getByIdClazz(Integer id) {
        return clazzMapper.getByIdClazz(id);
    }

    /**
     * 根据id删除班级
     */
    @Override
    public void deleteById(Integer id)   {
        Integer count = clazzMapper.selectCount(id);
        if (count > 0) {
            throw new CustomerExceptionHandler("对不起, 该班级下有学生, 不能直接删除");
        }
        clazzMapper.deleteById(id);
    }

    /**
     * 修改班级信息
     */
    @Override
    public void updateClazz(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.updateClazz(clazz);
    }

    /**
     * 查询学员管理中条件查询中的所有班级
     */
    @Override
    public List<Clazz> findAll() {
        return clazzMapper.findAll();

    }
}
